package com.yxjx.duoxue.ad;

import java.io.Serializable;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.JumpActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.datamodel.DMAdItem;
import com.yxjx.duoxue.mta.stat.StatHelper;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class AdPageActivity extends BaseActionBarActivity {

	private WebView webview;
	private String url;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_ad_page);
		mContext = this;
		url = getIntent().getExtras().getString(Yx.KEY_URL);
		String title = getIntent().getExtras().getString(Yx.KEY_TITLE);
		title = UIHelper.isEmpty(title) ? "活动" : title;
		customizeActionBarTitle(title);
		
		mActionBar.getCustomView().findViewById(R.id.share)
				.setVisibility(View.VISIBLE);

		// 可选项AdItem Object
		Serializable adObject = getIntent().getExtras().getSerializable(
				Yx.EXTRA_AD_OBJECT);
		if (adObject != null && adObject instanceof DMAdItem) {
			DMAdItem data = (DMAdItem) adObject;
			customizeShare(data.getPageUrl(), data);
			// 如果该item不支持分享，则不显示分享按钮。
			if (data.getIsShared() != 1) {
				mActionBar.getCustomView().findViewById(R.id.share).setVisibility(View.INVISIBLE);
			}
			
			//
			if (Utils.hasKeyValue(getIntent(), Yx.KEY_OPEN_BY, Yx.VALUE_OPEN_BY_AD_CLICK)) {
				StatHelper.addKVEvent(mContext, "event_home_ad_show", 
						StatHelper.KEY_AD, data.getPageUrl());
			}
		} else {
			mActionBar.getCustomView().findViewById(R.id.share)
					.setVisibility(View.INVISIBLE);
		}

		webview = (WebView) findViewById(R.id.webview);
		webview.getSettings().setAppCacheEnabled(true);
		webview.getSettings().setBuiltInZoomControls(false);
		webview.getSettings().setJavaScriptEnabled(true);

//		webview.setWebViewClient(new WebViewClient() {
//			
//			@Override
//			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				Utils.logd("CMM7 overrideUrlLoading url = " + url);
//				
//				if (!url.startsWith(JumpActivity.PREFIX_DATA)
//						&& !url.startsWith(JumpActivity.PREFIX_LINK)) {
//					// 非内部定义的schema选择重新加载
//					Utils.logd("CMM7 overrideUrlLoading load url = " + url);
//					view.loadUrl(url);
//					return true;
//				} else {
//					// 否则选择跳转
//					Utils.logd("CMM7 overrideUrlLoading skip url = " + url);
//					return false;
//				}
//			}
//			
//			
//		});
		
		webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				if (newProgress == 100) {
					findViewById(R.id.loading).setVisibility(View.INVISIBLE);
				}
			}
			
		});

		webview.loadUrl(url);
		findViewById(R.id.loading).setVisibility(View.VISIBLE);
	}

	/**
	 * 网络加载失败重新刷新页面
	 * 
	 * @param view
	 */
	public void toRefresh(View view) {
		webview.loadUrl(url);
		findViewById(R.id.loading).setVisibility(View.VISIBLE);
	}
}
